package be.djdb.manipulators.streams.util;

import java.util.logging.Level;
import java.util.logging.Logger;


public class Stringcontentrecorder {
	private char contentbuffer[];
	private char actifchar;
	private int reclength;//4=>>>>>img:<<<<<img/img.gif
	private int recklok;//actieve plaats van char recorder
	private char delemiter;//img:>>>>"<<<<img/img.gif>>>>"<<<<
	private int delemiterlenght;//img:>>>>img/img.gif<<<<
	private static boolean contentEnabled ;
	private boolean ismatch;
	private static final boolean DEBUG = false;
	public Stringcontentrecorder(char delemiter) {
		this.delemiter= delemiter;
		SetReset();
	}
	Stringcontentrecorder(char start,int length){
		reclength =length;
		this.delemiter= start;
		SetReset();
	}
	/*
	* @return true 
	*/
	public boolean contentfinder(int charint){
		actifchar = (char)charint;
		if (DEBUG){log(Level.WARNING, "find:"+ actifchar); }
			if (contentEnabled){
				if (delemiter== actifchar ){
					// 1 puls detectie Negatieve flank of delemiter char
					ismatch = true;
				}else{
					try{
						contentbuffer[recklok]=actifchar;
					}catch(ArrayIndexOutOfBoundsException e){
						makeitbigger();
						contentbuffer=makeitbigger();
						contentbuffer[recklok]=actifchar;
					}
					recklok++;
				}
				
			}
			if(!ismatch){
				if (delemiter== actifchar ){//start
					if (!contentEnabled){// Positieve flank of delemiter char
						contentEnabled=true;
						return true;
					}
		    	}
			}
			return contentEnabled;
	   }
	public void SetReset(){
		contentbuffer= new char[20];
		recklok = 0;//initializeren
		ismatch = false;
		contentEnabled=false;
	}
	private char[] makeitbigger (){
		char buffer[] = new char[contentbuffer.length + 20];
		System.arraycopy(contentbuffer, 0, buffer, 0, contentbuffer.length);//arraycopy(Object srcarray, int srcPos,Object destarray,int destPos,int length)
		return buffer;
	}
	private  void setenabled(boolean enabled){
		this.contentEnabled = enabled;
	}
	public boolean getenabled(){
	  return this.contentEnabled;
	}
	public boolean ismatchfound(){
		return ismatch;
	}
	private char[]trimarray(char[]array,int length){
		char buffer[] = new char[length];
		System.arraycopy(array, 0, buffer, 0, length);//arraycopy(Object srcarray, int srcPos,Object destarray,int destPos,int length)
		return buffer;
	}
    private static  void log(Level level, String msg ){
        String tag ="<>>>>*UpdateCV-L Roegiers 80072631156*<<<<>";
        Logger.getLogger(Stringcontentrecorder.class.getName()).log(level, tag + msg);
    }
    
	public char[]getvalue(){
		return trimarray(contentbuffer,recklok);
	}
}
